package vip.ifmm.lehouse.commodity.service.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import vip.ifmm.lehouse.commodity.service.api.CommodityTypeService;
import vip.ifmm.lehouse.commodity.service.dao.CommodityTypeDao;
import vip.ifmm.lehouse.commodity.service.utils.PageUtil;
import vip.ifmm.lehouse.common.dto.ServerResponse;
import vip.ifmm.lehouse.common.enums.CommonStatus;
import vip.ifmm.lehouse.common.enums.status.CommodityStatus;
import vip.ifmm.lehouse.common.enums.type.CommodityTypeStatusType;
import vip.ifmm.lehouse.common.model.CommodityType;
import vip.ifmm.lehouse.common.util.CodeUtil;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 商品分类服务实现类
 *
 * @author Fish
 * <p>Email: fishinlove@163.com</p>
 * <p>created by 2019/06/22 14:06:13</p>
 */
@Slf4j
@Service(owner = "fish", mock = "vip.ifmm.lehouse.commodity.service.api.mock.CommodityTypeServiceMock")
public class CommodityTypeServiceImpl implements CommodityTypeService {

    // 商品分类持久层接口
    @Resource
    private CommodityTypeDao commodityTypeDao = null;

    @Override
    @CacheEvict(cacheNames = "CommodityTypeService", allEntries = true)
    public ServerResponse<Void> saveCommodityType(CommodityType commodityType) {

        // 检验参数完整性
        if (CodeUtil.isBlank(commodityType.getCommodityType())) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_IS_MISSING);
        }

        // 初始化对象
        commodityType.setCommodityTypeStatus(CommodityTypeStatusType.NORMAL.getValue());
        commodityType.setCommodityTypeCreateTime(new Date());
        commodityType.setCommodityTypeUpdateTime(new Date());
        if (commodityTypeDao.saveCommodityType(commodityType) <= 0) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_SAVED_FAILED);
        }

        // 保存成功
        return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_SAVED);
    }

    @Override
    @CacheEvict(cacheNames = "CommodityTypeService", allEntries = true)
    public ServerResponse<Void> updateCommodityType(CommodityType commodityType) {

        // 检查完整性
        if (commodityType.getCommodityTypeId() == null) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_IS_MISSING);
        }

        // 初始化对象
        commodityType.setCommodityTypeUpdateTime(new Date());
        if (commodityTypeDao.updateCommodityType(commodityType) <= 0) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_UPDATED_FAILED);
        }

        return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_UPDATED);
    }

    @Override
    @CacheEvict(cacheNames = "CommodityTypeService", allEntries = true)
    public ServerResponse<Void> deleteCommodityType(CommodityType commodityType) {

        // 检查完整性
        if (commodityType.getCommodityTypeId() == null) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_IS_MISSING);
        }

        // 执行操作
        commodityType.setCommodityTypeUpdateTime(new Date());
        if (commodityTypeDao.removeCommodityType(commodityType) <= 0) {
            return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_DELETED_FAILED);
        }

        return ServerResponse.response(CommodityStatus.COMMODITY_TYPE_DELETED);
    }

    @Override
    @Cacheable(cacheNames = "CommodityTypeService")
    public ServerResponse<PageInfo<?>> listCommodityTypes(int pageNum,
                                                          int pageSize,
                                                          boolean needPaged) {

        if (needPaged) {
            // 需要分页
            PageUtil.toPage(pageNum, pageSize);
        }

        // 这里不管是否需要分页都统一返回 PageInfo 对象，这样前端统一使用 PageInfo 来进行渲染
        // 一旦后面需要进行分页，只需要多增加一个请求参数即可，不需要改动界面渲染代码
        return ServerResponse.response(CommonStatus.SUCCESS,
                PageUtil.pageInfo(commodityTypeDao.listCommodityTypes()));
    }

    @Override
    @Cacheable(cacheNames = "CommodityTypeService")
    public ServerResponse<CommodityType> getCommodityType(int commodityTypeId) {
        return ServerResponse.response(CommonStatus.SUCCESS, commodityTypeDao.getCommodityType(commodityTypeId));
    }
}
